package com.xiaojiezhu.codegen;

import com.xiaojiezhu.codegen.controller.TestController;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

/**
 * 作者 zxj<br>
 * 时间 2017/10/23 12:03
 * 说明 ...
 */
public class App extends Application {
    private final WebView webView = new WebView();
    private final WebEngine engine = webView.getEngine();

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.getIcons().add(new Image(getClass().getClassLoader().getResource("icon.png").toExternalForm()));
        Scene scene = new Scene(webView);
        primaryStage.setScene(scene);

        initWebEngine();

        primaryStage.setTitle("so");

        primaryStage.show();
    }

    private void initWebEngine() {
        JSObject window = (JSObject) engine.executeScript("window");
        window.setMember("testController",new TestController());

        engine.setOnAlert(new EventHandler<WebEvent<String>>() {
            @Override
            public void handle(WebEvent<String> event) {
                System.out.println(event.getData());
            }
        });
        engine.load(loadUrl("web/index.html"));
    }

    private String loadUrl(String url){
        return getClass().getClassLoader().getResource(url).toExternalForm();
    }
}
